package com.yanchangapp.ui.presenter;

import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.commom.MenuIdentify;
import com.yanchangapp.model.bean.ComposeMenuIcons;
import com.yanchangapp.model.bean.MenuIcons;
import com.yanchangapp.model.response.MenuResponse;
import com.yanchangapp.ui.fragment.home.HomeFragment;
import com.yanchangapp.ui.view.MgAtView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019/7/22.
 * description: 我的管理P层
 */
public class MgAtPresenter extends BasePresenter<MgAtView> {

    public MgAtPresenter(BaseActivity context) {
        super(context);
    }

    public void fetchList(){
        List<MenuResponse.Bean> mStaticMenuList = HomeFragment.mStaticMenuList;
        // 重新进行请求
        if (mStaticMenuList == null || mStaticMenuList.size() == 0){
            queryMenuLists();
        }else {
            parseList(mStaticMenuList);
        }
    }

    private void parseList(List<MenuResponse.Bean> mStaticMenuList) {
        List<ComposeMenuIcons> mTargetList = new ArrayList<>();
        if (mStaticMenuList == null || mStaticMenuList.size() == 0){
            getView().queryMenuListSuccess(mTargetList);
            return;
        }
        for (int i=0;i<mStaticMenuList.size();i++){
            MenuResponse.Bean firstBean = mStaticMenuList.get(i);
            // 进入我的管理层
            if (firstBean.getId() == MenuIdentify.MANAGE.id){
                // 我的管理第二层
                List<MenuResponse.Bean> secondLists = firstBean.getList();
                for (int j=0;j<secondLists.size();j++){
                    List<MenuIcons> menuIcons = new ArrayList<>();
                    menuIcons.clear();
                    List<MenuResponse.Bean> thirdLists = secondLists.get(j).getList();
                    for (int k=0;k<thirdLists.size();k++){
                        MenuResponse.Bean bean = thirdLists.get(k);
                        menuIcons.add(new MenuIcons(bean.getId(),bean.getName(),bean.getImageAddress(),bean.getMobileAddress()));
                    }
                    // 将第二层的名称以及列表进行加入....
                    mTargetList.add(new ComposeMenuIcons(secondLists.get(j).getName(),menuIcons));
                }
                break;
            }
        }
        getView().queryMenuListSuccess(mTargetList);
    }

    private void queryMenuLists(){
        HeadApiRetrofit.getInstance().queryMenuLists()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MenuResponse>() {
                    @Override
                    public void onNext(MenuResponse menuResponse) {
                        if (menuResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            HomeFragment.mStaticMenuList = menuResponse.getList();
                            parseList(menuResponse.getList());
                        }else {
                            getView().queryMenuListFailure(menuResponse.getMsg());
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        getView().queryMenuListFailure("获取企业采购列表失败");
                    }
                });
    }
}
